var superagent = require('superagent');
var cheerio = require('cheerio');

exports.detail = function (req, res, next) {
    console.log(req.url);
    superagent.get(`http://www.xdg555.com${req.url}`)
        .end(function (err, sres) {
            if (err) {
                return next(err);
            }
            // sres.text 里面存储着网页的 html 内容，将它传给 cheerio.load 之后
            // 就可以得到一个实现了 jquery 接口的变量，我们习惯性地将它命名为 `$`
            // 剩下就都是 jquery 的内容了
            var $ = cheerio.load(sres.text);
            var items = {};
            var introduce = []
            var way = []
            var videoResource = {}
            
            //获取详情页面
            $('.content #intro p').each(function (index, element) {
                var $element = $(element);
                introduce.push(
                    $element.text()
                );
            });
            //
            $('.card  .play_from li').each(function (index, element) {
                var $element = $(element);
                way.push({
                    id:index,
                    name: $element.text()
                });
            });
            let img = $('.v_content  .cover img').attr('src')
            let title = $('.v_title  a').text()
            let list = []
            $('.card  .v_list .cover').each(function (index, element) {
                var $element = $(element);
                list.push({
                    name:$element.next().text(),
                    href:$element.attr('href'),
                    desc:$element.next().next().text(),
                    src: $element.attr('data-bg')
                });
            });
            let listMes = []
            $('.v_desc').html().match(/em>.*/g).forEach(e=>{
                listMes.push(e.split('').slice(3).join(''))
            })
            let desc = $('.v_desc .desc').text()
            let a =[]
            let i = 0
            $('.tab_content .play_list  li').each(function (index, element) {
                var $element = $(element);
                if ($element.next().text()==='') {
                    console.log(index);
                    videoResource[i] = a
                    a=[]
                    i++
                }
                a.push({
                  "href":$element.find('a').attr('href'),
                  "content":$element.find('a').text()
                });
            });
            items ={
                "introduce":introduce,
                "way":way,
                "videoResource":videoResource,
                "listMes":listMes,
                "list":list,
                "descImg":img,
                "title":title,
                "desc":desc
            }
            res.send(items);
        });
}